<!DOCTYPE html>
          <head>
        <meta charset="utf-8">
            
            <title>
                「Android」*不同Context区别 | ttdevs
            </title>
            <meta content="width=device-width, initial-scale=1" name="viewport">
            <meta name="theme-color" content="#4184f3">
            
            
            <link href="/favicon.ico" rel="icon"/>
            

            <link rel="stylesheet" href="/css/highlight.light.css">
            <link rel="stylesheet" href="/css/prism-customize.css">
            <link rel="stylesheet" href="/css/nav-icon.css">
            <link rel="stylesheet" href="/css/waves.min.css">
            <link rel="stylesheet" href="/css/jquery.tocify.css">
            <link rel="stylesheet" href="/css/main.css">
            <link rel="stylesheet" href="/css/nav-indicator.css">
            
  

  
  <!-- 谷歌统计 -->
  <script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-97465173-1', 'auto');
    ga('send', 'pageview');

  </script>
  
            </meta>
        </meta>
    </head>

    <body>
        <header>
            <!-- cover image or sth. -->
        </header>
        <div id="main" class="m-scene">
            
<div class="nav-wrapper">

    <div class="container">
        <nav>
            <div class="logo wave">
                <a href="/" id="logo">
                    ttdevs
                </a>
            </div>
            <div class="nav-toggle-icon" >
                <div class="material-hamburger">
                    <span>
                    </span>
                    <span>
                    </span>
                    <span>
                    </span>
                </div>
            </div>
            <div class="menu-wrapper">
                <div class="nav-indicator">
                </div>
                <ul class="menus">
                    
                     
                        <li>
                            <a class="wave " href="/">
                                首页
                            </a>
                        </li>
                     
                        <li>
                            <a class="wave " href="/archives">
                                归档
                            </a>
                        </li>
                     
                        <li>
                            <a class="wave " href="/about">
                                关于
                            </a>
                        </li>
                     
                    
                   
                </ul>
            </div>
        </nav>
    </div>
</div>
            <div class="container content">
                <div class="scene_element scene_element--fadein">
                    <div class="row">
    <div class="main">
        <article>
          
          <header class="post-header with-cover" style="background-image:url('/1970/01/01/[Android]不同Context区别/cover.jpg')" >
          
          </header>
          <h1 class="post-title">「Android」*不同Context区别</h1>

          <section class="post-info">
            <span class="post-date">1970/01/01</span>
            
            <span class="post-category">
                <a class="article-category-link" href="/categories/技术/">技术</a>
            </span>
            
            
            <span class="post-tags">
              <ul class="post-tag-list"><li class="post-tag-list-item"><a class="post-tag-list-link" href="/tags/Android/">Android</a></li></ul>
            </span>
            
          </section>

          <section class="post-content">
            <p>代码：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">AlertDialog dialog = <span class="keyword">new</span> AlertDialog</div><div class="line">    .Builder(context)</div><div class="line">    .setTitle(title)</div><div class="line">    .setCancelable(<span class="keyword">false</span>)</div><div class="line">    .setPositiveButton(<span class="string">"确定"</span>, listener)</div><div class="line">    .create();</div><div class="line">dialog.show();</div></pre></td></tr></table></figure>
<p>错误信息：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">android.view.WindowManager$BadTokenException: Unable to add window -- token <span class="keyword">null</span> is not <span class="keyword">for</span> an </div><div class="line">	at android.view.ViewRootImpl.setView(ViewRootImpl.java:<span class="number">589</span>)</div><div class="line">	at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:<span class="number">326</span>)</div><div class="line">	at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:<span class="number">224</span>)</div><div class="line">	at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:<span class="number">149</span>)</div><div class="line">	at android.app.Dialog.show(Dialog.java:<span class="number">293</span>)</div><div class="line">	at com.ttdevs.easysecuritysmartbar.StartHandler.handleMessage(StartHandler.java:<span class="number">39</span>)</div><div class="line">	at android.os.Handler.dispatchMessage(Handler.java:<span class="number">99</span>)</div><div class="line">	at android.os.Looper.loop(Looper.java:<span class="number">137</span>)</div><div class="line">	at android.app.ActivityThread.main(ActivityThread.java:<span class="number">4866</span>)</div><div class="line">	at java.lang.reflect.Method.invokeNative(Native Method)</div><div class="line">	at java.lang.reflect.Method.invoke(Method.java:<span class="number">511</span>)</div><div class="line">	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:<span class="number">786</span>)</div><div class="line">	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:<span class="number">553</span>)</div><div class="line">	at dalvik.system.NativeStart.main(Native Method)</div></pre></td></tr></table></figure>
<p>其中context是通 <code>getApplicationContext()</code> 获取的，将其换成 <code>Activity</code> 问题解决。</p>
<h2 id="0xFF-参考"><a href="/1970/01/01/[Android]不同Context区别/#0xFF-参考" class="headerlink" title="0xFF 参考"></a>0xFF 参考</h2><ol>
<li><a href="http://blog.csdn.net/lmj623565791/article/details/40481055" target="_blank" rel="external">http://blog.csdn.net/lmj623565791/article/details/40481055</a></li>
</ol>

          </section>
        </article>
        

       
        <div class="pager">
          
            <a class="post-prev pager-item" href="/1970/01/01/[Android]Toobar的一个简单封装/" >
              <strong class="article-nav-caption">上一篇</strong>
              <p class="post-nav-title">「Android」Toobar的一个简单封装</p>
            </a>
          
          
            <a class="post-next pager-item" href="/1970/01/01/[Android]LayoutInflate遇到的坑/">
              <strong class="article-nav-caption">下一篇</strong>
              <p class="post-nav-title">「Android」LayoutInflate遇到的坑</p>
            </a>
          
        </div>
        

         <!-- comments -->
        <div class="comment-section">
  
    


</div>

    </div>
    
    <aside>
        <div id="toc">
        </div>
    </aside>
    
</div>

                </div>
            </div>
        </div>
        <footer class="footer">
    <p>由<a href="http://hexo.io/" target="_blank">Hexo</a>强力驱动，搭载<a href="https://github.com/wayou/hexo-theme-gstyle">gstyle</a>主题</p>
    <p>
        &copy; 2017 ttdevs
    </p>
</footer>
<script src="/lib/jquery.js"></script>
<script src="/lib/waves.js"></script>
<script src="/lib/jquery-ui.js"></script>
<script src="/lib/jquery.tocify.js"></script>
<script src="/js/main.js"></script>

    </body>
</html>
